#include <unistd.h>
#include <glib.h>
#include <json-glib/json-glib.h>
#include <json-glib/json-gobject.h>

int main(int argc, char **argv)
{
  JsonParser *parser = NULL;
  JsonGenerator *gen = NULL;
  JsonNode *root = NULL, *node = NULL;
  JsonObject *object = NULL, *send_obj = NULL, *send_value = NULL;
  JsonArray *data = NULL, *value = NULL, *column = NULL, *send_data = NULL;
  GError *error = NULL;
  gsize len = 0, i = 0, sum = 0, j = 0, sum_column = 0;//rc = 0, ;
  const gchar *str_column, *str_value;
  gchar *str = NULL;

  g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);

  parser = json_parser_new();
  //json_parser_load_from_data(parser, buffer, -1, &error);
  json_parser_load_from_file(parser, "6.json", &error);
  if (error)
  {
    g_error ("%s\n", error->message);
    exit (1);
  }
  root = json_parser_get_root(parser);
  object = json_node_get_object(root);
  data = json_object_get_array_member(object, "result_station");
  if (!data)
  {
    g_error ("%s\n", error->message);
    exit (1);
  }
  sum = json_array_get_length(data);
  send_obj = json_object_new();
  send_data = json_array_new();
  json_object_set_array_member(send_obj, "data", send_data);
  for (i = 0; i < sum; i++) {
    send_value = json_object_new();
    json_array_add_object_element(send_data, send_value);
    value = json_array_get_array_element(data, i);
    column = json_array_get_array_element(data, 0);
    sum_column = json_array_get_length(column);
    for (j = 0; j < sum_column; j++) {
      str_column = json_array_get_string_element(column, j);
      str_value = json_array_get_string_element(value, j);
      if (str_value)
      {
        g_debug("[%d]str:%s\n", __LINE__, str_value);
        json_object_set_string_member(send_value, str_column, str_value);
      }
    }
  }
  /*
  gen = json_generator_new();
  node = json_node_new(JSON_NODE_OBJECT);
  json_node_take_object(node, send_obj);
  json_generator_set_root (gen, node);
  str = json_generator_to_data (gen, &len);
  */
  str = json_to_string (root, TRUE);
  g_debug("%d:[len:%ld]%s\n", __LINE__, len, str);
//out:
  if (parser)
    g_object_unref (parser);
  if (gen)
    g_object_unref (gen);
  if (node)
    json_node_free (node);
  return 1;
}
